#include <cctype>
class Solution {
public:
    bool IPv4(const string& ip) {
        if (ip.front() == '.' || ip.back() == '.') return false;
        int count = 0;
        int start = 0;
        while (count < 3) {
            size_t pos = ip.find(".", start);
            if (pos == string::npos) return false;
            count++;
            string s = ip.substr(start, pos-start);
            start = pos+1;
            if (s.size() > 3) return false;
            else {
                if (s[0] == '0') return false;
                else {
                    for (char c : s) {
                        if (!isdigit(c)) return false;
                    }
                    int num = stoi(s);
                    if (num <= 0 || num > 255) return false;
                } 
            }
        }
        return true;
    }
    bool IPv6(const string& ip) {
        if (ip.front() == ':' || ip.back() == ':') return false;
        int count = 0;
        int start = 0;
        while (count < 7) {
            size_t pos = ip.find(":", start);
            if (pos == string::npos) return false;
            count++;  
            if (ip.substr(pos, 2) == "::") return false;
            string s = ip.substr(start, pos-start);
            start = pos+1;
            if (s.size() > 4) return false;
            for (char c : s) {
                if (!isxdigit(c)) return false;
            }
        }
        return true;
    }
    string solve(string ip) {
        if (IPv4(ip)) return "IPv4";
        else if (IPv6(ip)) return "IPv6";
        else return "Neither";
    }
};